package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.dto.FieldInfo;
import com.sg.common.exception.BizException;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.QuerySystemDockingPersonSourceDataDetailReq;
import com.sg.dto.base.framework.req.QuerySystemDockingPersonSourceDataListReq;
import com.sg.entity.PersonSourceData;
import com.sg.mapper.PersonSourceDataMapper;
import com.sg.service.base.framework.MPersonSourceDataService;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MPersonSourceDataServiceImpl
        extends ServiceImpl<PersonSourceDataMapper, PersonSourceData>
        implements MPersonSourceDataService {

    @Autowired
    PersonSourceDataMapper personSourceDataMapper;

    /**
     * 1-3-18批量增系统对接员工源数据[7404]
     * gen by moon at 9/16/2023, 2:37:47 PM
     */
    @Trace(operationName = "1-3-18批量增系统对接员工源数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddSystemDockingPersonSourceData(List<PersonSourceData> personSourceData) {

        if (personSourceData == null || personSourceData.size() == 0) {
            return false;
        }

        return super.saveBatch(personSourceData.stream().map(item -> {
            item.setPersonSourceDataId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
            }
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 1-3-18查询系统对接员工源数据列表[7406]
     * gen by moon at 9/16/2023, 2:37:50 PM
     */
    @Trace(operationName = "1-3-18查询系统对接员工源数据列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PersonSourceData> querySystemDockingPersonSourceDataList(QuerySystemDockingPersonSourceDataListReq reqDto) {

        try {
            QueryWrapper<PersonSourceData> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PersonSourceData.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PersonSourceData.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PersonSourceData.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<PersonSourceData>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-18查询系统对接员工源数据详情[7407]
     * gen by moon at 9/16/2023, 2:37:53 PM
     */
    @Trace(operationName = "1-3-18查询系统对接员工源数据详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PersonSourceData querySystemDockingPersonSourceDataDetail(QuerySystemDockingPersonSourceDataDetailReq reqDto) {

        try {

            QueryWrapper<PersonSourceData> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<PersonSourceData> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                PersonSourceData retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
